rm(list = ls())

setwd('/path/to/replication/')

library(data.table)
library(zoo)
library(strucchange)
library(ggplot2)

# Figure B11: Generalized fluctuation test for structural breaks in the number of negative articles about Chinese- and African-origin people
articles <- fread('./data/newspaper_articles_sentiment_classification.csv')

articles[,month:=as.yearmon(paste(month(date), year(date), sep='-'), format='%m-%Y')]

## Figure B11 (a)
## break in trends
asian <- articles[group=='china' & sentiment_negative==1]
asian <- asian[,.N,by=month]
asian <- asian[order(month)]
asian_ts <- ts(asian[,N], start=c(2018, 1), end=c(2020, 12), frequency=12) 

#### testing for structural change (residuals)
re.seat <- efp(asian_ts ~ 1, type='OLS-MOSUM')

## dating the structural change
bp.seat <- breakpoints(asian_ts ~ 1, type='OLS-MOSUM')

## plot structural change 
pdf('./chinese_papers_trend_break.pdf', width=6, height=4.5)
plot(re.seat, xlab=NULL, main = NA, axes=F, ylab="Chinese negative articles' fluctuation process")
box(bty="l")
axis(2, c(-1.2, 0, 1.2))
axis(1, c(2018, 2019, 2020, 2021))
lines(bp.seat, breaks = 1,col='blue')
dev.off()

# Chow test on Jan, 2020
sctest(asian_ts ~ 1,
       type = "Chow", point = 25)

## Figure B11 (b)
## break in trends
africa <- articles[group=='africa' & sentiment_negative==1]
africa <- africa[,.N,by=month]
africa <- africa[order(month)]
africa_ts <- ts(africa[,N], start=c(2018, 1), end=c(2020, 12), frequency=12) 

#### testing for structural change (residuals)
re.seat <- efp(africa_ts ~ 1, type='OLS-MOSUM')

## dating the structural change
bp.seat <- breakpoints(africa_ts ~ 1, type='OLS-MOSUM')

## plot structural change 
pdf('./african_papers_trend_break.pdf', width=6, height=4.5)
plot(re.seat, xlab=NULL, main = NA, axes=F, ylab="African negative articles' fluctuation process")
box(bty="l")
axis(2, c(-1.2, 0, 1.2))
axis(1, c(2018, 2019, 2020, 2021))
lines(bp.seat, breaks = 1,col='blue')
dev.off()

# Chow test on Jan, 2020
sctest(africa_ts ~ 1,
       type = "Chow", point = 25)
